import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println(list);
        //遍历方法1：for循环
        for(int i=0;i<list.size();i++){
            System.out.print(list.get(i)+" ");
        }
        System.out.println();
        //遍历方法2：for each
        for(Integer n:list){
            System.out.print(n+" ");
        }
        System.out.println();
        //遍历方法3：迭代器1
        Iterator<Integer> it=list.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
        //遍历方法4：迭代器2
        ListIterator<Integer> lis=list.listIterator();
        while(lis.hasNext()){
            System.out.print(lis.next()+" ");
        }
        System.out.println();
        //遍历方法5：迭代器反向遍历
        ListIterator<Integer> listPrev=list.listIterator(list.size());
        while(listPrev.hasPrevious()){
            System.out.print(listPrev.previous()+" ");
        }
        System.out.println();
    }
}